<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="/component/pear/css/pear.css"/>
    <style>
        label.required:after { /* 给红色的必填项打红色星号（文字后）如果打前面用before*/
            content: ' *';
            color: red;
        }

        .layui-form-label {
            height: 50px;
            line-height: 45px;
            padding: 0px;
        }

        .layui-form .layui-input-inline {
            margin-left: 30px;
            width: 290px;
        }

        .layui-laydate {
            font-size: 16px;
        }

        .layui-laydate-footer span {
            font-size: 15px;
            height: 30px;
            line-height: 30px;
        }

        .laydate-footer-btns {
            position: absolute;
            right: 10px;
            top: 7px;
        }

        .main-container .layui-form-item {
            margin: 20px 0px !important;
        }

        .layui-input {
            height: 50px;
        }

        .layui-btn, .pear-btn {
            height: 40px;
            width: 110px;
            font-size: 18px;
        }

        .pear-btn i {
            font-size: 20px;
        }

        .layui-form-select dl, .dtree-select {
            position: absolute;
            left: 0;
            top: 51px;
        }

    </style>
</head>
<body>
<div class="main-container"
     style="display: flex;flex-direction: column;justify-content: center;">
    <div class="layui-row">
        <div class="layui-col-xs12 layui-col-sm12 layui-col-md12" style="font-size: 16px;">
            <form class="layui-form" action="" onsubmit="return false">
                <div class="layui-form-item">
                    <label class="layui-form-label required">连接名称</label>
                    <div class="layui-input-block">
                        <input type="text" name="name" lay-verify="required" lay-reqtext="请输入连接名称" placeholder="请输入" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label required">连接驱动</label>
                    <div class="layui-input-block">
                        <input type="text" name="className" lay-verify="required" lay-reqtext="请输入连接驱动" placeholder="请输入" class="layui-input" value="com.mysql.cj.jdbc.Driver" readonly>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label required">主机</label>
                    <div class="layui-input-block">
                        <input type="text" name="host" lay-verify="required" lay-reqtext="请输入主机" placeholder="请输入" class="layui-input" value="localhost">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label required">端口</label>
                    <div class="layui-input-block">
                        <input type="text" name="port" lay-verify="required" lay-reqtext="请输入端口" placeholder="请输入" class="layui-input" value="3306">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label required">用户名</label>
                    <div class="layui-input-block">
                        <input type="text" name="username" lay-verify="required" lay-reqtext="请输入用户名" placeholder="请输入" class="layui-input" value="root">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label required">密码</label>
                    <div class="layui-input-block">
                        <input type="text" name="password" lay-verify="required" lay-reqtext="请输入密码" placeholder="请输入" class="layui-input" value="123456">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">备注</label>
                    <div class="layui-input-block">
                        <textarea name="remark" placeholder="请输入" class="layui-input" style="resize: none; min-height: 100px"></textarea>
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-input-block">
                        <button class="layui-btn pear-btn-md layui-btn-normal" lay-submit lay-filter="connectBtn">
                            测试连接
                        </button>
                        <button class="layui-btn pear-btn-md layui-btn-normal" lay-submit lay-filter="saveBtn">
                            保存
                        </button>
                    </div>
                </div>
            </form>
        </div>

    </div>
</div>
<script src="/component/layui/layui.js"></script>
<script src="/component/pear/pear.js"></script>
<script>
    layui.use(['form', 'jquery', 'laydate','toast'], function () {
        let form = layui.form;
        let $ = layui.jquery;
        let laydate = layui.laydate;
        var util = layui.util;
        var toast = layui.toast;

        // 测试连接
        form.on('submit(connectBtn)', function (data) {
            $.ajax({
                url: '/backup/node/info/connect',
                data: JSON.stringify(data.field),
                dataType: 'json',
                contentType: "application/json;charset=utf-8",
                type: 'post',
                success: function (result) {
                    if (result.code === 200) {
                        layer.msg(result.message, {icon: 1, time: 1800});
                    } else {
                        layer.msg(result.message, {icon: 2, time: 1800});
                    }
                }
            })
            return false;
        });

        // 提交事件
        form.on('submit(saveBtn)', function (data) {
            let loading = layer.load();
            $.ajax({
                url:'/backup/node/info/add',
                data:JSON.stringify(data.field),
                dataType:'json',
                contentType: "application/json;charset=utf-8",
                type:'post',
                success:function(result){
                    layer.close(loading);
                    if(result.code === 200){
                        layer.msg(result.message,{icon:1,time:1800},function(){
                            parent.layer.close(parent.layer.getFrameIndex(window.name));//关闭当前页
                        });
                    }else{
                        layer.msg(result.message,{icon:2,time:1800});
                    }
                }
            })
            return false;
        });
    });
</script>
</body>
</html>